/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.umss.arqui10;

import edu.umss.arqui10.modelo.Account;
import edu.umss.arqui10.modelo.History;
import edu.umss.arqui10.modelo.Holder;
import edu.umss.arqui10.modelo.OutputBean;

import edu.umss.arqui10.modelo.Registry;
import edu.umss.arqui10.modelo.Responsible;
import edu.umss.arqui10.modelo.Service;
import edu.umss.arqui10.modelo.ServiceReceiver;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeMap;

/**
 *
 * @author Ninten
 */
public class JAlgo {
    //Rho

    private TreeMap<Long, Service> services;
    private TreeMap<Long, ServiceReceiver> serviceReceivers;
    private LinkedList<ServiceReceiver> serviceReceiverList;
    private LinkedList<OutputBean> outputBeans;
    private LinkedList<Holder> holders;
    private LinkedList<Account> accounts;
    private TreeMap<Integer, Responsible> responsibleList;

    public JAlgo() {
        services = new TreeMap<Long, Service>();
        outputBeans = new LinkedList<OutputBean>();
    }

    public OutputBean addOutputBean(String name, String description, Long id, BigDecimal price) {
        //guardar
        OutputBean ob = new OutputBean(name, description, id, price);
        outputBeans.add(ob);
        return ob;
    }

    public OutputBean getOutputBean(Long id) {
        for (OutputBean ob : outputBeans) {
            if (ob.getId().equals(id)) {
                return ob;
            }
        }
        return null;
    }

    public boolean changeCurrentPrice(Long id, BigDecimal price) {
        OutputBean ob = getOutputBean(id);
        if (ob != null) {
            ob.changeCurrentPrice(price);
            return true;
        }
        return false;
    }

    public boolean changeName(Long id, String name) {
        OutputBean ob = getOutputBean(id);
        if (ob != null) {
            ob.setName(name);
            return true;
        }
        return false;
    }

    public boolean changeDescription(Long id, String description) {
        OutputBean ob = getOutputBean(id);
        if (ob != null) {
            ob.setDescription(description);
            return true;
        }
        return false;
    }

    public boolean setUsable(Long id, boolean enable) {
        OutputBean ob = getOutputBean(id);
        if (ob != null) {
            ob.setUsable(enable);
            return true;
        }
        return false;
    }

    //Rho
    public void addService(Long id, String name, String desc, BigDecimal t) {
        Service newService = new Service(id, name, desc, t);
        services.put(id, newService);
    }

    //Rho
    public Collection getServices() {
        return this.services.values();
    }

    //Rho
    public void removeService(Long id) {
        this.services.get(id).setUsable(false);
    }

    public void assignServiceAt(Long idRS, Long idServ) {
        ServiceReceiver sr = this.serviceReceivers.get(idRS);
        Service service = this.services.get(idServ);
        sr.assignService(service);
    }

    public ServiceReceiver addServiceReceiver(String name) {

        ServiceReceiver serviceReceiver = null;

        Iterator<ServiceReceiver> it = serviceReceiverList.iterator();
        while (it.hasNext()) {
            ServiceReceiver sr = it.next();
            if (sr.getName().equalsIgnoreCase(name)) {
                serviceReceiver = sr;
            }
        }
        if (serviceReceiver == null) {
            serviceReceiver = new ServiceReceiver(name);
        }
        return serviceReceiver;
    }

    public LinkedList<ServiceReceiver> findServiceReceiversByName(String name) {
        LinkedList<ServiceReceiver> list = new LinkedList<ServiceReceiver>();
        Iterator<ServiceReceiver> it = serviceReceiverList.iterator();
        while (it.hasNext()) {
            ServiceReceiver sr = it.next();
            if (sr.getName().equalsIgnoreCase(name)) {
                list.add(sr);
            }
        }
        return list;
    }

    public History getHistory(String receiverName) {
        History h = null;
        Iterator<ServiceReceiver> it = serviceReceiverList.iterator();
        while (it.hasNext()) {
            ServiceReceiver sr = it.next();
            if (sr.getName().equalsIgnoreCase(receiverName)) {
                h = sr.getHistory();
            }
        }
        return h;
    }

    public TreeMap<Date, Registry> getRegistries(ServiceReceiver sr) {
        TreeMap<Date, Registry> registries = sr.getHistory().getAllRegistry();
        return registries;
    }

    public Holder addHolder(Long id) {
        Holder h = null;
        for (int i = 0; i < holders.size(); i++) {
            Holder holder = holders.get(i);
            if (holder.getId() == id) {
                h = holder;
            }
        }
        if (h == null) {
            h = new Holder(id);
        }
        return h;
    }

    public Holder getHolderById(Long id) {
        Holder h = null;
        for (int i = 0; i < holders.size(); i++) {
            Holder holder = holders.get(i);
            if (holder.getId() == id) {
                h = holder;
            }
        }
        return h;
    }

    public Holder getHolderByName(String name) {
        Holder h = null;

        for (int i = 0; i < holders.size(); i++) {
            Holder holder = holders.get(i);
            if (holder.getName().equalsIgnoreCase(name)) {
                h = holder;
            }
        }
        return h;
    }

    public Account getAccountByHolder(Holder h) {
        return h.getAccount();
    }

    public void removeHolder(String name) {
        for (int i = 0; i < holders.size(); i++) {
            Holder holder = holders.get(i);
            if (holder.getName().equalsIgnoreCase(name)) {
                holder = null;
            }
        }
    }

    public Responsible getResponsibleById(Integer id){
        return responsibleList.get(id);
    }

    public Responsible getResponsibleByName(String name){
        Collection c = responsibleList.values();
        Iterator<Responsible> it = c.iterator();
        Responsible res = null;
        while(it.hasNext()){
            res = ((Responsible)it.next());
            if(res.getName().equals(name)){
                break;
            }
        }
        if(res==null){
            throw new UnsupportedOperationException("Responsible not found (ByName)");
        }
        return res;
    }
}
